/** 
 * Project Name:zju 
 * File Name:AdminController.java 
 * Package Name:cn.edu.zju.controller 
 * Date:2014年10月9日下午2:00:58 
 * Copyright (c) 2014, 杭州杰唐信息技术有限公司 All Rights Reserved. 
 * 
 */
package com.hbjycl.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;









import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbjycl.entity.Admin;
import com.hbjycl.entity.Student;
import com.hbjycl.service.AdminService;
import com.hbjycl.service.StudentService;

/**
 * 用户管理
 * 
 * @author wxp
 *
 * @datetime 2014年10月9日下午2:00:58
 */
@Controller
public class StudentController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);
    
    @Resource
    private StudentService        studentService;
    /*
     * 注册页面
     */
    @RequestMapping(value = "/registerview", method = RequestMethod.GET)
    public ModelAndView registerview(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("register");
        return mav;
    }
    /*
     * 注册
     */
	@RequestMapping(value = "student/register", method = RequestMethod.POST)
    public ModelAndView register(HttpSession httpsession,
            @RequestParam(value = "username", required = false, defaultValue = "") String username,
            @RequestParam(value = "password", required = false, defaultValue = "") String password,
            @RequestParam(value = "mobile", required = false, defaultValue = "") String mobile,
            @RequestParam(value = "email", required = false, defaultValue = "") String email,
            @RequestParam(value = "name", required = false, defaultValue = "") String name
         ){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        Student student=studentService.getStudentByUsername(username);
        Student newstudent=new Student();
        if(student==null)
        {   	
        	newstudent.setUsername(username);
        	newstudent.setPassword(password);
        	newstudent.setMobile(mobile);
        	newstudent.setEmail(email);
        	newstudent.setName(name);
        	try{
                studentService.save(newstudent);
                mav.addObject("username", newstudent.getUsername());
                httpsession.setAttribute("username", newstudent.getUsername());
        	}
          catch(Exception e)
        	{
        	  System.out.println(e);
        	  mav.addObject(TIP_ERROR,e);
          	  mav.setViewName("index");
        	}
        }
        else
        {
        	mav.addObject(TIP_ERROR,"用户名已存在");
        	mav.setViewName("register");
        }
        return mav;
    }
/*	  
     * 登录页面
  */   
	   @RequestMapping(value = "loginview", method = RequestMethod.GET)
	    public ModelAndView loginview(HttpSession session, HttpServletRequest request)
	    {
	        ModelAndView mav = new ModelAndView();
	        mav.setViewName("login");
	        return mav;
	    }
	  /*
     * 登录
     */
	   @RequestMapping(value = "student/login", method = RequestMethod.POST)
	    public ModelAndView login(HttpSession httpsession,
	    		@RequestParam(value = "username", required = false, defaultValue = "") String username,
	            @RequestParam(value = "password", required = false, defaultValue = "") String password)
	    {
	        ModelAndView mav = new ModelAndView();
	        mav.setViewName("index");
	        Student student=studentService.getStudentByUsername(username);
	        if(student==null)
	        {
	        	mav.addObject(TIP_ERROR, "用户名不存在");
	        	mav.setViewName("login");
	        }
	        else if(password.equals(student.getPassword()))
	        {
	        	mav.addObject("username", student.getUsername());
	        	mav.addObject(TIP_INFO, "登陆成功");
	        	httpsession.setAttribute("username", student.getUsername());
	        }
	        else
	        {
	        	mav.addObject(TIP_ERROR, "用户名或密码错误");
	        	mav.setViewName("login");
	        }
	        return mav;
	    }
    
}
